import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import axios from 'axios';
import {Tool} from "@/util/tool";
import hljs from 'highlight.js' //导入代码高亮文件
import 'highlight.js/styles/atom-one-light.css' //导入代码高亮样式

axios.defaults.baseURL = process.env.VUE_APP_SERVER;

//创建v-contentPreTreatment全局指令
const app = createApp(App);
app.directive('contentPreTreatment', function (el) {
    //处理内容大纲
    const setElementId = (element: any) => {
        if (element.length > 0) {
            for (let i = 0; i < element.length; i++) {
                const item = element.item(i);
                item.id = item.innerText;
            }
        }
    }
    const h2 = document.getElementsByTagName("h2");
    setElementId(h2);
    const h3 = document.getElementsByTagName("h3");
    setElementId(h3);

    //处理代码高亮
    const blocks = el.querySelectorAll('pre code');
    blocks.forEach((block: any) => {
        hljs.highlightBlock(block);
    })
})

app.use(store).use(router).use(Antd).mount('#app')

console.log('环境：', process.env.NODE_ENV);
console.log('服务端：', process.env.VUE_APP_SERVER);

/**
 * axios拦截器
 */
axios.interceptors.request.use(function (config) {
    console.log('请求参数：', config);
    const token = store.state.user.token;
    if (Tool.isNotEmpty(token)) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        config.headers.token = token;
        console.log("请求headers增加token：", token);
    }
    return config;
}, error => {
    return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
    console.log('返回结果：', response);
    return response;
}, error => {
    console.log('返回错误：', error);
    return Promise.reject(error);
});
